import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;


public class OptionWindow {

	public JFrame frame;
	DefaultListModel lm;
	DefaultListModel lm2;
	
	String thePerson;
	String id;
	JList options;
	Database db;
	
	ArrayList<Person> results;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					OptionWindow window = new OptionWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public OptionWindow() {
		initialize();
		String[] theOptions = {"Date of Birth", "Date of Death", "Gender", "Address", "Bio"};
		populateOptions(theOptions);
		db = new Database();
	}
	
	public void populate(ArrayList<Person> p){
		lm.clear();
		for(int i = 0; i < p.size(); i++){
			String name = p.get(i).fname + " " + p.get(i).lname;
			lm.addElement(name);
		}
		if(p.size() == 0){
			lm.addElement("None");
		}
	}
	
	public void populate(String item){
		lm.clear();
		lm.addElement(item);
	}
	
	public void populateOptions(String[] newOpts){
		lm2.clear();
		for(int i = 0; i < newOpts.length; i++){
			lm2.addElement(newOpts[i]);
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 517, 316);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		thePerson = MainScreen.personSelected;
		id = MainScreen.personId;
		frame.setTitle(thePerson);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(282, 92, 178, 154);
		frame.getContentPane().add(scrollPane);
		
		lm = new DefaultListModel();
		lm2 = new DefaultListModel();
		
		JList resultList = new JList(lm);
		scrollPane.setViewportView(resultList);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(32, 92, 103, 154);
		frame.getContentPane().add(scrollPane_1);
		
		options = new JList(lm2);
		scrollPane_1.setViewportView(options);
		
		JButton btnQuery = new JButton(">> Query >>");
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(options.getSelectedValue() != null){
					if(options.getSelectedValue().equals("Parents")){
						results = db.getParents(id);
						populate(results);
					}else if(options.getSelectedValue().equals("Father")){
						results = db.getParent(id,"f");
						populate(results);
					}else if(options.getSelectedValue().equals("Mother")){
						results = db.getParent(id,"m");
						populate(results);
					}else if(options.getSelectedValue().equals("Children")){
						results = db.getChildren(id);
						populate(results);
					}else if(options.getSelectedValue().equals("Siblings")){
						results = db.getSiblings(id);
						populate(results);
					}else if(options.getSelectedValue().equals("Brothers")){
						results = db.getSibling(id,"b");
						populate(results);
					}else if(options.getSelectedValue().equals("Sisters")){
						results = db.getSibling(id,"s");
						populate(results);
					}else if(options.getSelectedValue().equals("Grandparents")){
						results = db.getGrandparents(id);
						populate(results);
					}else if(options.getSelectedValue().equals("Grandchildren")){
						results = db.getGrandchildren(id);
						populate(results);
					}else if(options.getSelectedValue().equals("Shared Birthdays")){
						results = db.getSharedBDays(id);
						if(results == null){
							String res = "None";
							populate(res);
						}else{
							populate(results);
						}
					}else if(options.getSelectedValue().equals("Spouse")){
						results = db.getMarriedTo(id);
						if(results == null){
							String res = "None";
							populate(res);
						}else{
							populate(results);
						}
					}else if(options.getSelectedValue().equals("Divorced")){
						results = db.getDivorced(id);
						if(results == null){
							String res = "Not Divorced";
							populate(res);
						}else{
							populate(results);
						}
					}else if(options.getSelectedValue().equals("Adopted By")){
						results = db.getAdoptedBy(id);
						if(results == null){
							String res = "Not Adopted";
							populate(res);
						}else{
							populate(results);
						}
					}else if(options.getSelectedValue().equals("Date of Birth")){
						String result = db.getDOB(id);
						String newResult = "";
						if(!result.equals("NA")){
							String delims = "[-]+";
							String[] res = result.split(delims);
							if(res[1].equals("01")){
								newResult += "January ";
							}else if(res[1].equals("02")){
								newResult += "February ";
							}else if(res[1].equals("03")){
								newResult += "March ";
							}else if(res[1].equals("04")){
								newResult += "April ";
							}else if(res[1].equals("05")){
								newResult += "May ";
							}else if(res[1].equals("06")){
								newResult += "June ";
							}else if(res[1].equals("07")){
								newResult += "July ";
							}else if(res[1].equals("08")){
								newResult += "August ";
							}else if(res[1].equals("09")){
								newResult += "September ";
							}else if(res[1].equals("10")){
								newResult += "October ";
							}else if(res[1].equals("11")){
								newResult += "November ";
							}else if(res[1].equals("12")){
								newResult += "December ";
							}
							newResult += res[0] + ", ";
							newResult += res[2];
						}else{
							newResult = "Unknown";
						}
						populate(newResult);
					}else if(options.getSelectedValue().equals("Date of Death")){
						String result = db.getDOD(id);
						String newResult = "";
						if(!result.equals("NA")){
							String delims = "[-]+";
							String[] res = result.split(delims);
							if(res[1].equals("01")){
								newResult += "January ";
							}else if(res[1].equals("02")){
								newResult += "February ";
							}else if(res[1].equals("03")){
								newResult += "March ";
							}else if(res[1].equals("04")){
								newResult += "April ";
							}else if(res[1].equals("05")){
								newResult += "May ";
							}else if(res[1].equals("06")){
								newResult += "June ";
							}else if(res[1].equals("07")){
								newResult += "July ";
							}else if(res[1].equals("08")){
								newResult += "August ";
							}else if(res[1].equals("09")){
								newResult += "September ";
							}else if(res[1].equals("10")){
								newResult += "October ";
							}else if(res[1].equals("11")){
								newResult += "November ";
							}else if(res[1].equals("12")){
								newResult += "December ";
							}
							newResult += res[0] + ", ";
							newResult += res[2];
						}else{
							newResult = "Unknown";
						}
						populate(newResult);
					}else if(options.getSelectedValue().equals("Gender")){
						String result = db.getGender(id);
						if(!result.equals("NA")){
							if(result.equals("M")){
								result = "Male";
							}else if(result.equals("F")){
								result = "Female";
							}
						}else{
							result = "Unknown";
						}
						populate(result);
					}else if(options.getSelectedValue().equals("Address")){
						String result = db.getAddress(id);
						if(result.equals("NA")){
							result = "Unknown";
						}
						populate(result);
					}else if(options.getSelectedValue().equals("Bio")){
						String result = db.getBio(id);
						if(result.equals("NA")){
							result = "None";
						}
						populate(result);
					}
				}
			}
		});
		btnQuery.setBounds(145, 125, 127, 23);
		frame.getContentPane().add(btnQuery);
		
		JButton family = new JButton("Family");
		family.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lm.clear();
				String[] theOptions = {"Parents", "Father", "Mother", "Spouse", "Children", "Siblings", "Brothers", "Sisters", "Grandparents", "Grandchildren"};
				populateOptions(theOptions);
			}
		});
		family.setBounds(32, 34, 103, 23);
		frame.getContentPane().add(family);
		
		JButton person = new JButton("Person Info");
		person.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lm.clear();
				String[] theOptions = {"Date of Birth", "Date of Death", "Gender", "Address", "Bio"};
				populateOptions(theOptions);
			}
		});
		person.setBounds(32, 11, 103, 23);
		frame.getContentPane().add(person);
		
		JButton other = new JButton("Other");
		other.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lm.clear();
				String[] theOptions = {"Shared Birthdays", "Divorced", "Adopted By"};
				populateOptions(theOptions);
			}
		});
		other.setBounds(32, 58, 103, 23);
		frame.getContentPane().add(other);
	}
}
